package org.cwnuit.train.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

@SuppressWarnings("all")
public class CheckboxTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** checkbox name的值 **/
	private String name;

	/** checkbox id的值 **/
	private String id;

	/** checkbox 全部数据 **/
	private Map<String, String> alldata;

	/** checkbox 被选中的数据 **/
	private Map<String, String> data;

	@Override
	public int doEndTag() throws JspException {
		/** 输出checkbox html **/
		JspWriter out = pageContext.getOut();
		try {
			StringBuffer sb = new StringBuffer();

			if (alldata != null && alldata.size()>=1) {

				List<String> allkeylist = new ArrayList(alldata.keySet());
				List<String> keylist = new ArrayList(data.keySet());

				for (String key : allkeylist) {
					sb.append("<span>");
					sb.append("<input type='checkbox' name='");
					sb.append(name);
					sb.append("' id='");
					sb.append(id);
					sb.append("' value='");
					sb.append(key);

					if (null!=keylist&& data.size()>=1 && keylist.contains(key)) {
						sb.append("' checked='checked'>");
					} else {
						sb.append("'>");
					}
					
					String value = alldata.get(key); // 值
					
					sb.append(value);
					sb.append("</span>&nbsp;&nbsp;&nbsp;");
				}
			}
			out.print(sb.toString());
		} catch (IOException e) {

			e.printStackTrace();
		}
		return super.doEndTag();
	}

	@Override
	public int doStartTag() throws JspException {
		return EVAL_BODY_AGAIN;
	}

	@Override
	public int doAfterBody() throws JspException {

		return EVAL_PAGE;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Map<String, String> getAlldata() {
		return alldata;
	}

	public void setAlldata(Map<String, String> alldata) {
		this.alldata = alldata;
	}

	public Map<String, String> getData() {
		return data;
	}

	public void setData(Map<String, String> data) {
		this.data = data;
	}

}
